package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.entity.*;
import com.burgeon.backend.EnterpriseWeChat.rest.VipInfo.*;
import com.burgeon.backend.EnterpriseWeChat.rest.vip.GetVipByEmp;
import com.burgeon.backend.EnterpriseWeChat.rest.vip.GetVipByEmpByPage;
import com.burgeon.backend.EnterpriseWeChat.rest.vip.GetVipByPhoneAndCardNo;
import com.burgeon.backend.EnterpriseWeChat.rest.vip.QueryVipByVipId;
import com.burgeon.backend.EnterpriseWeChat.service.EWAppEmpService;
import com.burgeon.backend.EnterpriseWeChat.service.EWVipService;
import com.burgeon.backend.EnterpriseWeChat.service.ex.UpDataException;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class EWVipServiceImpl implements EWVipService {
    private final static Log logger = LogFactory.getLog(EWVipService.class);
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public ArrayList<Vip> GetVipByEmp(String token, String empId, int page) {
        Info info = GetInfoByToken(token);
        GetVipByEmp getVipByEmp = new GetVipByEmp(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<Vip> vips = new ArrayList<>();
        try {
            vips = getVipByEmp.getData(empId, page);
//            for (Vip vip:vips) {
//                if("null".equals(vip.getLogoImg())){
//                    vip.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
//                }
//            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException("我的顾客错误"+e.getMessage());
        }

        return vips;
    }

    @Override
    public ArrayList<Vip> getVipByEmpByRange(String token, String empId, int range, int start) {
        Info info = GetInfoByToken(token);
        GetVipByEmpByPage getVipByEmp = new GetVipByEmpByPage(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<Vip> vips = new ArrayList<>();
        try {
            vips = getVipByEmp.getData(empId,range,start);

        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException("我的顾客错误"+e.getMessage());
        }

        return vips;
    }

    @Override
    public Vip GetVipByPhoneAndCardNo(String token, String content,String empId) {
        Info info = GetInfoByToken(token);
        GetVipByPhoneAndCardNo getVipByPhoneAndCardNo = new GetVipByPhoneAndCardNo(info.getUrl(),info.getAccount(),
                info.getPassword());
        Vip vip = new Vip();
        try {
             vip = getVipByPhoneAndCardNo.getData(content,empId);
            System.err.println(vip);
            if(vip == null){
//                System.err.println(vip);
                return null;
            }else {
                if ("null".equals(vip.getLogoImg())) {
                    vip.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
                }
                return vip;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 会员页面
     * @param cardNo
     * @return
     */
    @Override
    public Vip QueryVipByVipId(String token,String cardNo) {
        Info info = GetInfoByToken(token);
        QueryVipByVipId queryVipByVipId = new QueryVipByVipId(info.getUrl(),info.getAccount(),info.getPassword());
        Vip vip = null;
        try {
            vip = queryVipByVipId.getData(cardNo);
//            if("null".equals(vip.getLogoImg())){
//                vip.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vip;
    }

    /**
     * 色型质
     * @param token
     * @param vipNoOrPh
     * @return
     */
    @Override
    public VipSkinInfo queryUserSkinInfo(String token, String vipNoOrPh) {
        Info info = GetInfoByToken(token);
//        VipSkinInfo vipSkinInfo = new VipSkinInfo();
        queryUserSkinInfo queryUserSkinInfo = new queryUserSkinInfo(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            VipSkinInfo vipSkinInfo = queryUserSkinInfo.getData(vipNoOrPh);
            return vipSkinInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 保存色型质
     * @param token
     * @param vipSkinInfo
     * @param id
     * @return
     */
    @Override
    public String saveUserSkinInfo(String token, VipSkinInfo vipSkinInfo, String id) {
        Info info = GetInfoByToken(token);
        String objId = null;
        saveUserSkinInfo saveUserSkinInfo = new saveUserSkinInfo(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            objId =  saveUserSkinInfo.getData(vipSkinInfo, id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException("保存色型质信息错误"+e.getMessage());
        }
        return objId;
    }

    @Override
    public ArrayList<Feature> queryVipFeature(String token, String type) {
        Info info = GetInfoByToken(token);
        queryVipFeature queryVipFeature = new queryVipFeature(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<Feature> features = new ArrayList<>();
        try {
            features = queryVipFeature.getData(type);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return features;
    }

    /**
     * 家属信息
     * @param token
     * @param vipId
     * @return
     */
    @Override
    public ArrayList<VipFamily> queryFamilyInfo(String token, String vipId) {
        Info info = GetInfoByToken(token);
        ArrayList<VipFamily> families = new ArrayList<>();
        queryFamilyInfo queryFamilyInfo = new queryFamilyInfo(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            families = queryFamilyInfo.getData(vipId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return families;
    }

    @Override
    public String addFamilyInfo(String token,String cardNo, VipFamily vipFamily) {
        Info info = GetInfoByToken(token);
        addFamilyInfo addFamilyInfo = new addFamilyInfo(info.getUrl(),info.getAccount(),info.getPassword());
        String objId = null;
        try {
            objId =  addFamilyInfo.getData(cardNo, vipFamily);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException("添加家属信息错误"+e.getMessage());
        }
        return objId;
    }

    @Override
    public String processFamilyInfo(String token, VipFamily vipFamily) {
        Info info = GetInfoByToken(token);
        String objId = null;
        processFamilyInfo processFamilyInfo = new processFamilyInfo(info.getUrl(),info.getAccount(),info.getPassword());
        try {
             objId =processFamilyInfo.getData(vipFamily);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException("修改家属信息错误"+e.getMessage());

        }
        return objId;
    }

    @Override
    public ArrayList<VipRelation> queryRelationList(String token) {
        Info info = GetInfoByToken(token);
        ArrayList<VipRelation> vipRelations = new ArrayList<>();
        queryRelationList queryRelationList = new queryRelationList(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            vipRelations = queryRelationList.getData();
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
        return vipRelations;
    }

    @Override
    public void deleteItem(String token, String id, String table) {
        Info info = GetInfoByToken(token);
        deleteItem deleteItem = new deleteItem(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            deleteItem.getData(id, table);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("删除错误"+table+":"+e.getMessage());
            throw new UpDataException("删除错误"+table+":"+e.getMessage());
        }

    }

    @Override
    public VipFeature queryVipFeatureValue(String token, String vipId) {
        Info info = GetInfoByToken(token);
        queryVipFeatureValue queryVipFeatureValue = new queryVipFeatureValue(info.getUrl(),info.getAccount(),info.getPassword());
        VipFeature vipFeature= new VipFeature();
        try {
            vipFeature=queryVipFeatureValue.getData(vipId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vipFeature;
    }

    @Override
    public ArrayList<BeautyFeature> queryBeautyFeature(String token,String vipId) {
        Info info = GetInfoByToken(token);
        ArrayList<BeautyFeature> beautyFeatures = new ArrayList<>();
        queryVipFeatureValue queryVipFeatureValue = new queryVipFeatureValue(info.getUrl(),info.getAccount(),info.getPassword());
        queryBeautyFeature queryBeautyFeature = new queryBeautyFeature(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            VipFeature vipFeature=queryVipFeatureValue.getData(vipId);
            beautyFeatures = queryBeautyFeature.getData();
            System.err.println(beautyFeatures);
//            System.err.println(vipFeature.getHeight().equals("null"));
            for (BeautyFeature beautyFeature:beautyFeatures) {

                beautyFeature.setDimflag(beautyFeature.getDimflag().replace("属性","DIM"));

                    if ( beautyFeature.getDimflag().contains("27")&&!"null".equals(vipFeature.getHeight())) {
                        beautyFeature.setValue(vipFeature.getHeight());
                    } else if ( beautyFeature.getDimflag().contains("28")&&!"null".equals(vipFeature.getWeight())) {
                        beautyFeature.setValue(vipFeature.getWeight());
                    } else if ( beautyFeature.getDimflag().contains("29")&&!"null".equals(vipFeature.getBodyType())) {
                        beautyFeature.setValue(vipFeature.getBodyType());
                    } else if ( beautyFeature.getDimflag().contains("30")&&!"null".equals(vipFeature.getFeature())) {
                        beautyFeature.setValue(vipFeature.getFeature());
                    } else if ( beautyFeature.getDimflag().contains("31")&&!"null".equals(vipFeature.getShinColor())){
                        beautyFeature.setValue(vipFeature.getShinColor());
                    }


            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }

        return beautyFeatures;
    }

    @Override
    public String saveFeature(String token, String cardNo, String vipId, String valueName, String name) {
        Info info = GetInfoByToken(token);
        //判断是否有
        queryIsVipExist queryIsVipExist = new queryIsVipExist(info.getUrl(),info.getAccount(),info.getPassword());
        //新增
        saveFeature saveFeature = new saveFeature(info.getUrl(),info.getAccount(),info.getPassword());
        //修改
        modifyFeature modifyFeature = new modifyFeature(info.getUrl(),info.getAccount(),info.getPassword());
        if (valueName.contains("27")) {
            valueName = "C_VIPATTRIB27_ID__NAME";
        } else if (valueName.contains("28")) {
            valueName = "C_VIPATTRIB28_ID__NAME";
        } else if (valueName.contains("29")) {
            valueName = "C_VIPATTRIB29_ID__NAME";
        } else if (valueName.contains("30")) {
            valueName = "C_VIPATTRIB30_ID__NAME";
        } else {
            valueName = "C_VIPATTRIB31_ID__NAME";
        }
        String objId = null;
        try {
            String id = queryIsVipExist.getData(vipId);
            if( id!= null){
                //修改
               modifyFeature.getData(id,name,valueName);
            }else {
               objId = saveFeature.getData(cardNo,valueName,name);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
        return objId;
    }

    @Override
    public HashMap queryFeatureValue(String token, String dimflagId) {
        Info info = GetInfoByToken(token);
        HashMap map = new HashMap();
        ArrayList<DimFlag> dimFlags = new ArrayList<>();
        queryFeatureValue queryFeatureValue = new queryFeatureValue(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            dimFlags = queryFeatureValue.getData(dimflagId);
            map.put("DimFlags",dimFlags);
        } catch (Exception e) {
            e.printStackTrace();
        }

        map.put("dimflag",dimflagId);
        return map;
    }

    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }
}
